/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * formPhieuNhapSach.java
 *
 * Created on Jun 22, 2013, 3:41:06 PM
 */

package GUI;

import BUS.DauSachBUS;
import BUS.NhaXuatBanBUS;
import BUS.PhieuNhapSachBUS;
import BUS.SachBUS;
import Entity.CTPhieuNhapSach;
import Entity.DauSach;
import Entity.NhaXuatBan;
import Entity.PhieuNhapSach;
import Entity.Sach;
import Util.ComboBoxTagModel;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author copang
 */
public class formPhieuNhapSach extends javax.swing.JPanel {
    
    private DauSachBUS dauSachBUS;
    private SachBUS sachBUS;
    private NhaXuatBanBUS nhaXuatBanBUS;
    private PhieuNhapSachBUS phieuNhapSachBUS;
    
    private ComboBoxTagModel cmbTenSachModels;
    private DefaultTableModel tbChiTietModels = null;
    
    private ArrayList listDauSach;    

    /** Creates new form formPhieuNhapSach */
    public formPhieuNhapSach() {
        initComponents();        
        dauSachBUS = new BUS.DauSachImpl();   
        sachBUS = new BUS.SachImpl();
        nhaXuatBanBUS = new BUS.NhaXuatBanImpl();
        phieuNhapSachBUS = new BUS.PhieuNhapSachImpl();
        
        txtNhanVien.setText(Util.InstanceData.loginNhanVien.getHoTenNV());
        txtNamXB.setTransferHandler(null);
        txtSoLuong.setTransferHandler(null);
        txtDonGia.setTransferHandler(null);
        
        create();        
        updateNXB();
        setEnableControls(false);
    }
    
    protected void create() {
        initDate();
        loadTenSach();
        createTable();
    }
    
    protected void createTable() {
        if (tbChiTietModels == null) {
            tbChiTietModels = (DefaultTableModel) tbChiTiet.getModel();
        } else {
            tbChiTietModels.getDataVector().removeAllElements();
        }
    }
    
    protected void addTableModel() {        
        String MaDauSach = cmbTenSachModels.getTagItem(cmbTenSach.getSelectedIndex()).toString();
        String TenSach = cmbTenSach.getSelectedItem().toString();
        String NXB = txtNXB.getText();
        String SoLuong = txtSoLuong.getText();
        String GiaBan = txtDonGia.getText();        
        int thanhTien = Integer.parseInt(SoLuong) * Integer.parseInt(GiaBan);
        // add to table
        Object[] data = {
            MaDauSach,
            TenSach,
            NXB,
            txtNamXB.getText(),
            SoLuong,
            GiaBan,
            String.valueOf(thanhTien)
        };
        tbChiTietModels.addRow(data);
        
        // update ThanhTien        
        thanhTien += Integer.parseInt(txtThanhTien.getText());
        txtThanhTien.setText(String.valueOf(thanhTien));
        
        clearThongTin();
    }
    
    protected void removeTableModel(int index) {
        int thanhTien = Integer.parseInt(tbChiTietModels.getValueAt(index, 6).toString());
        thanhTien = Integer.parseInt(txtThanhTien.getText()) - thanhTien;
        txtThanhTien.setText(String.valueOf(thanhTien));
        
        tbChiTietModels.removeRow(index);
    }
    
    protected void clearThongTin() {        
        txtSoLuong.setText("0");
        txtDonGia.setText("0");        
    }
    
    protected void clearTable() {
        tbChiTietModels.getDataVector().clear();
        txtThanhTien.setText("0");                
    }
    
    protected void setEnableControls(boolean isEnable) {
        btnLuuPhieu.setEnabled(isEnable);
        btnXoaPhieu.setEnabled(isEnable);
        btnThem.setEnabled(isEnable);
    }
    
    protected void clear() {
        // clear thong tin
        clearThongTin();
        
        // clear table
        clearTable();
        
        // disable control
        setEnableControls(false);
    }
    
    protected void initDate() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        txtNgayLap.setText(dateFormat.format(date));
    }
    
    protected void loadTenSach() {
        listDauSach = dauSachBUS.getAll();        
        
        cmbTenSachModels = new ComboBoxTagModel();
        for (int i=0; i<listDauSach.size(); i++) {
            DauSach tg = (DauSach) listDauSach.get(i);
            cmbTenSachModels.addElement(tg.getTenSach(), tg.getMaDauSach());
        }
        cmbTenSach.setModel(cmbTenSachModels);
    }

    protected void updateNXB() {
        int index = cmbTenSach.getSelectedIndex();
        int MaNXB = ((DauSach)listDauSach.get(index)).getMaNXB();
        
        NhaXuatBan data = nhaXuatBanBUS.getByID(MaNXB);
        if (data != null) {
            txtNXB.setText(data.getTenNXB());
        }
    }    
    
    protected boolean createSach(Sach sach, int row) {
        sach.setMaSach(0);
        sach.setMaDauSach( Integer.parseInt(tbChiTietModels.getValueAt(row, 0).toString()) );
        sach.setGiaBia(Integer.parseInt(tbChiTietModels.getValueAt(row, 5).toString()));
        sach.setNamXB( Integer.parseInt(tbChiTietModels.getValueAt(row, 3).toString()) );
        sach.setSoLuongTon( Integer.parseInt(tbChiTietModels.getValueAt(row, 4).toString()) );
        
        sach.setBiaSau("");
        sach.setBiaTruoc("");
        sach.setGhiChu("");
        
        return sachBUS.add(sach);
    }
    
    protected boolean createCTPhieuNhap(int MaPhieu, int MaSach, int SoLuong, int Gia) {
        CTPhieuNhapSach data = new CTPhieuNhapSach(MaPhieu, MaSach, SoLuong, Gia);
        return phieuNhapSachBUS.addDetail(data);
    }
    
    protected boolean save() {
        // create PhieuNhapSach get MaPhieuNhap
        PhieuNhapSach phieuNhap = new PhieuNhapSach(
                0,Util.InstanceData.loginNhanVien.getMaNhanVien(),
                txtNgayLap.getText(), Integer.parseInt(txtThanhTien.getText()));        
        
        if ( !phieuNhapSachBUS.add(phieuNhap) ) {
            return false;
        }
        
        // select each row in table
        for (int i=0; i<tbChiTietModels.getRowCount(); i++) {
            
            // save Sach get MaSach
            Sach sach = new Sach();
            if ( !createSach(sach, i) ) return false;
            
            // save CT_PhieuNhapSach
            if ( !createCTPhieuNhap(phieuNhap.getMaPhieuNhapSach(), sach.getMaSach(), 
                    sach.getSoLuongTon(), sach.getGiaBia()) )
                return false;
        }
        
        return true;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel18 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbChiTiet = new javax.swing.JTable();
        cmbTenSach = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        txtThanhTien = new javax.swing.JTextField();
        txtNhanVien = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        txtNgayLap = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        txtDonGia = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtSoLuong = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtNXB = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtNamXB = new javax.swing.JTextField();
        btnThem = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        btnPhieuMoi = new javax.swing.JButton();
        btnLuuPhieu = new javax.swing.JButton();
        btnXoaPhieu = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();

        jLabel18.setText("Ngày lập phiếu :");

        tbChiTiet.setModel(new javax.swing.table.DefaultTableModel(
            null,
            new String [] {
                "Mã đầu sách", "Tên sách", "Nhà xuất bản", "Năm xuất bản", "Số lượng (Quyển)", "Đơn giá (VND)", "Thành Tiền"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbChiTiet.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbChiTietKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbChiTiet);

        cmbTenSach.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cmbTenSach.setOpaque(false);
        cmbTenSach.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbTenSachItemStateChanged(evt);
            }
        });

        jLabel12.setText("Tên đầu sách :");

        txtThanhTien.setEditable(false);
        txtThanhTien.setForeground(new java.awt.Color(255, 0, 0));
        txtThanhTien.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtThanhTien.setText("0");

        txtNhanVien.setEditable(false);

        jLabel16.setText("Thành tiền :");

        jLabel17.setText("Nhân viên lập phiếu :");

        txtNgayLap.setEditable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Chi Tiết Sách"));

        txtDonGia.setText("0");
        txtDonGia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtDonGiaKeyTyped(evt);
            }
        });

        jLabel7.setText("Năm xuất bản :");

        txtSoLuong.setText("0");
        txtSoLuong.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtSoLuongKeyTyped(evt);
            }
        });

        jLabel6.setText("Số lượng :");

        jLabel5.setText("Đơn giá :");

        jLabel8.setText("Nhà xuất bản :");

        txtNXB.setEditable(false);

        jLabel9.setText("Quyển");

        jLabel10.setText("VND");

        txtNamXB.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNamXBKeyTyped(evt);
            }
        });

        btnThem.setText("Thêm chi tiết");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtNXB, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
                        .addComponent(txtNamXB))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnThem)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtDonGia, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
                                .addComponent(txtSoLuong)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel9))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNXB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNamXB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel6))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnThem)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jToolBar1.setRollover(true);

        btnPhieuMoi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/Add.png"))); // NOI18N
        btnPhieuMoi.setText("Phiếu Mới");
        btnPhieuMoi.setFocusable(false);
        btnPhieuMoi.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        btnPhieuMoi.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnPhieuMoi.setInheritsPopupMenu(true);
        btnPhieuMoi.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnPhieuMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPhieuMoiActionPerformed(evt);
            }
        });
        jToolBar1.add(btnPhieuMoi);

        btnLuuPhieu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/save.png"))); // NOI18N
        btnLuuPhieu.setText("Lưu Phiếu");
        btnLuuPhieu.setFocusable(false);
        btnLuuPhieu.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnLuuPhieu.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnLuuPhieu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnLuuPhieuMouseClicked(evt);
            }
        });
        jToolBar1.add(btnLuuPhieu);

        btnXoaPhieu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/delete.png"))); // NOI18N
        btnXoaPhieu.setText("Xóa Phiếu");
        btnXoaPhieu.setFocusable(false);
        btnXoaPhieu.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnXoaPhieu.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnXoaPhieu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaPhieuActionPerformed(evt);
            }
        });
        jToolBar1.add(btnXoaPhieu);

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/update.png"))); // NOI18N
        btnUpdate.setText("Cập Nhật");
        btnUpdate.setFocusable(false);
        btnUpdate.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnUpdate.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnUpdate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnUpdateMouseClicked(evt);
            }
        });
        jToolBar1.add(btnUpdate);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel18)
                    .addComponent(jLabel17)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNgayLap, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbTenSach, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(36, 36, 36)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(193, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1007, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel16)
                        .addGap(18, 18, 18)
                        .addComponent(txtThanhTien, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 1017, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(cmbTenSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNgayLap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel18))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel17)
                            .addComponent(txtNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtThanhTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel16))))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cmbTenSachItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbTenSachItemStateChanged
        updateNXB();
    }//GEN-LAST:event_cmbTenSachItemStateChanged

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        addTableModel();
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnLuuPhieuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLuuPhieuMouseClicked
        if ( !save() ) {
            JOptionPane.showMessageDialog(this, "Không thể nhập phiếu");            
        } else {
            JOptionPane.showMessageDialog(this, "Đã nhập phiếu thành công");
            clear();            
        }                
    }//GEN-LAST:event_btnLuuPhieuMouseClicked

    private void btnPhieuMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPhieuMoiActionPerformed
        setEnableControls(true);
    }//GEN-LAST:event_btnPhieuMoiActionPerformed

    private void btnXoaPhieuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaPhieuActionPerformed
        clear();
    }//GEN-LAST:event_btnXoaPhieuActionPerformed

    private void txtNamXBKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNamXBKeyTyped
        Util.InstanceData.check(evt);
    }//GEN-LAST:event_txtNamXBKeyTyped

    private void txtSoLuongKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSoLuongKeyTyped
        Util.InstanceData.check(evt);
    }//GEN-LAST:event_txtSoLuongKeyTyped

    private void txtDonGiaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDonGiaKeyTyped
        Util.InstanceData.check(evt);
    }//GEN-LAST:event_txtDonGiaKeyTyped

    private void tbChiTietKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbChiTietKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
            int index = tbChiTiet.getSelectedRow();
            if (index >= 0)
                removeTableModel(index);
        }
    }//GEN-LAST:event_tbChiTietKeyPressed

    private void btnUpdateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnUpdateMouseClicked
        loadTenSach();
    }//GEN-LAST:event_btnUpdateMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLuuPhieu;
    private javax.swing.JButton btnPhieuMoi;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JButton btnXoaPhieu;
    private javax.swing.JComboBox cmbTenSach;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tbChiTiet;
    private javax.swing.JTextField txtDonGia;
    private javax.swing.JTextField txtNXB;
    private javax.swing.JTextField txtNamXB;
    private javax.swing.JTextField txtNgayLap;
    private javax.swing.JTextField txtNhanVien;
    private javax.swing.JTextField txtSoLuong;
    private javax.swing.JTextField txtThanhTien;
    // End of variables declaration//GEN-END:variables

}
